<?php
namespace app\admin\controller;
use app\admin\model\Customers;
use app\admin\model\Users;
use app\admin\controller\AdminAuth;
use think\Validate;
use think\Image;
use think\Db;
use think\config;
use think\Request;
use app\admin\collection\AliyunOss;

class AllproductsController extends AdminAuth
{
	//模块基本信息
	private $data = array(
		'module_name' => '所有拍品',
		'module_url'  => '/admin/allproducts/',
		'module_slug' => 'Customers',
		'upload_path' => UPLOAD_PATH,
		'upload_url'  => '/public/uploads/',
    );
	//所有线上拍品列表
	public function index(){
        //$conn = config::get('db_guwan');
        //$conDb = Db::connect('db_guwan');
        $where = [];
        input('mobile') ? $where['U.mobile'] = input('mobile') : false; //手机号
        input('goods_name') ? $where['G.goods_name'] = ['like','%'.input('goods_name').'%'] : false; //商品名称
        if(input('startTime') && input('endTime')){
            $start = strtotime(input('startTime'));
            $end = strtotime(input('endTime'));
            $where['G.upload_time'] = ['between',[$start, $end]];
        }
        if(input('up_down') == 1){   // 上架中 1
            $where['G.endTime'] = ['gt',time()];
        }elseif(input('up_down') == 2){  //下架中
            $where['G.endTime'] = ['lt',time()];
        }

        $allproduct = Db::connect('db_guwan')->name('goods')
            ->alias('G')
            ->join('users U','G.user_id = U.user_id')
            ->field('G.*,U.nickname,U.mobile,U.sex,U.head_pic')
            ->where($where)
            ->order('goods_id DESC')
            ->paginate(10);


        $this->assign('allproduct',$allproduct);
        $this->assign('data',$this->data);
        return view();
    }

    //线上拍品详情
    public function productDetail(Request $request){
	    //$ip = $request->ip();
	    $goods_id = input('goods_id');
	    $goodsDetail = Db::connect('db_guwan')->name('goods')
            ->alias('G')
            ->join('users U','G.user_id = U.user_id')
            ->field('G.*,U.nickname,U.mobile,U.sex,U.head_pic')
            ->where(['goods_id'=>$goods_id])
            ->find();


	    $goods_images = Db::connect('db_guwan')->name('goods_images')
            ->field('image_url')
            ->where(['goods_id'=>$goods_id])
            ->select();
	    foreach($goods_images as $k=>$v){
            $res_id = $v['image_url'];
            $goods_images[$k]['image_url'] = (new AliyunOss(false))->getCurlofimg(Config::get('aliyun.bucket'), $res_id, 31536000);
        }

	    $this->assign([
            'data' => $this->data,
            'goodsDetail' => $goodsDetail,
            'goods_images' => $goods_images
        ]);

        return view('detail');
    }

    //所有线上用户
    public function onlineUser(){
        $where = [];
        input('mobile') ? $where['mobile'] = input('mobile') : false; //手机号
        input('nickname') ? $where['nickname'] = input('nickname') : false; //昵称
        input('is_enter',1) ? input('is_enter') : false; //是否录入

        if(input('is_enter',1)){ //默认筛选
            //已经录入的所有客户电话
            $customers = Db::name('customers')->order('customers_id ASC')->column('mobile');
            //未分配客户
            $uncustomers = Db::name('uncustomers')->order('uncustomers_id ASC')->column('mobile');
            //合并
            $allcustomer = array_merge($customers,$uncustomers);
            //线上的所有用户
            $online = Db::connect('db_guwan')->name('users')->order('user_id ASC')->column('mobile');
            $onlineuser = array_filter($online);
            //筛选差异
            $re_arr = array_diff($onlineuser,$allcustomer);
            //dump($re_arr);exit;
            if($re_arr != NULL){
                $onlines = Db::connect('db_guwan')->name('users')
                    ->where($where)
                    ->where('mobile','in',$re_arr)
                    ->select();
            }else{
                $onlines = [];
            }

        }else{
            $onlines = Db::connect('db_guwan')->name('users')
                ->where($where)
                ->order('user_id DESC')
                ->paginate();
        }


        $this->assign([
            'onlines' => $onlines,
            'data' =>$this->data
        ]);

        return view();

    }

    //导入未分配表中
    public function waituser(Request $request){
        $users_id_array = $_GET['users_id_array'];
        $users_id_array = explode(',',$users_id_array);
        if (!empty($users_id_array)) {
            $user_where = array(
                'user_id' => array('IN', $users_id_array)
            );
            $waituser = Db::connect('db_guwan')->name('users')->field('user_id,nickname')->where($user_where)->select();
        }
        $this->assign('waituser',$waituser);

        return view('action');
    }
    //导入未分配表中
    public function dowaituser(Request $request){
        $user_id = input('post.')['user_id']; //二维数组
        foreach($user_id as $v){
            $waituser = Db::connect('db_guwan')->name('users')->field('nickname,mobile,head_pic,store_level,sex')->where(['user_id'=>$v])->find();
            $data = [
                'c_nickname' => $waituser['nickname'],
                'mobile' => $waituser['mobile'],
                'sex' => $waituser['sex'],
                'header_pic' => $waituser['head_pic'],
                'customer_levlel' => $waituser['store_level'],
                'addtime' => time(),
            ];
            $re = Db::name('uncustomers')->insert($data);
        }
        return json(['code'=>'1','msg'=>'操作成功']);

    }


}